<{capture name="header"}>
<style>
.order-box{ height:auto; padding:10px; margin:5px 20px; border:1px solid #ddd}
.order-box li{ padding:3px 0}
.order-tr{  font-size:14px; font-weight:bold; border-bottom:1px solid #ddd}
.table-border{ margin:10px 0; border-top:2px solid #333;border-bottom:2px solid #333}
.order-box td{ padding:3px 5px}
.order-font{ font-size:14px; font-weight:bold; padding:0 5px; clear:both}

#express_tools{z-index:65535; position:absolute;right:0 ;margin-right:20px; background:#FFFFFF;border:#CCCCCC 1px solid; }
#express_tools_hidden{padding:10px; width:10px; }
#express_tools_main{padding:10px; width:280px;; z-index:65535; position:absolute;right:0 ;margin-right:20px; background:#FFFFFF;border:#CCCCCC 1px solid; }
.express_tools_main_head{ 
    cursor: move;
    font-size: 14px;
    font-weight: bold;
    height: 27px;
    line-height: 27px;
    overflow: hidden;
    padding: 0 8px;
}


</style>
<{/capture}>

<script type="text/javascript">
var err = <{$err}>;
if (err==true){
	new Dialog(new Element("div.tableform",'<div class="division">部分发货单已被合并或者拆分</div><div class="table-action"><{button label="关闭" onclick="re_finder();"}></div>'),{
        title:'提示',
        width:230,
        height:130,
        modal:true,
        resizeable:false});
}

function re_finder(){
    opener.finderGroup['<{$env.get.finder_id}>'].unselectAll();
    opener.finderGroup['<{$env.get.finder_id}>'].refresh.delay(400,opener.finderGroup['<{$env.get.finder_id}>']);
    window.close();
}
var count = <{$count}>;

function check(){
    var value = 1;
    for (i=1;i<=count;i++){
        if ($("txt"+i).value == ''){
            alert("请填写完所有发货单对应的快递单号");
            return false;
        }
    }
    $('form1').submit();
}
$("cc").addEvent('change',function(e){
    if (this.checked){
        $ES("img[id^=bbaa]").set('styles',{'display':''});
        $('sp_1').set('text','开启上下关联');
    }else {
        $ES("img[id^=bbaa]").set('styles',{'display':'none'});
        $('sp_1').set('text','关闭上下关联');
    }
});
$ES("input[id^=txt]").each(function(el,i){
    el.addEvent('keydown',function(e){
        if(e.code==13){
                var a = i+1;
                if ($ES("input[id^=txt]").length != a){
                        $ES("input[id^=txt]")[a].focus();
                }else if($ES("input[id^=txt]").length == a){
					check();
				}
            return false;
        }
    });
});

$('form1').store('target',{
    onRequest:function(){
       $('b2').set('disabled', 'true');
    },
    onComplete:function(jsontext){
       var json = Json.evaluate(jsontext);
       if (typeof(json.error) != 'undefined'){
           $('b2').set('disabled', '');
       }else{
           $('b2').set('disabled', 'true');
           opener.finderGroup['<{$env.get.finder_id}>'].refresh.delay(400,opener.finderGroup['<{$env.get.finder_id}>']);
           window.close();
       }
    }
});

//--------------------------------快递单号关联
//上下关联
function updown_express(btn){
	next_express(btn);
	prev_express(btn);
}
//向下关联
function next_express(btn){
    var value = btn;
    if ($("txt"+value).value == ''){
        alert("请扫描快递单号");
        return;
    }
    var str = $("txt"+value).value;

    var express_company_no = '<{$express_company_no}>';
    if (express_company_no){
   	   switch(express_company_no){
         case 'EMS'://EMS
         	 if (isEMSNo(str)){
         		 enterNextNo(btn,express_company_no);
                 return ;
             }
         	 break;
         case 'SF'://顺丰
        	 if (isSFNo(str)){
		         enterNextNo(btn,express_company_no);
	             return ;
        	 }
             break;
         case 'ZJS'://宅急送
             if (isZJSNo(str)){
	             enterNextNo(btn,express_company_no);
	             return ;
             }
             break;
        }
    }
    var end = str.substr(-1);
    if(!is_num(end)){
        alert('请输入正确的快递单号!');
        return ;
    }
    num = str.match(/[1-9]\d*\b/g);
    len = str.lastIndexOf(num);
    start = str.substring(0,len);
    n = new Number(num)+1;
    while (true){
        var aa = value+1;
        if ($("txt"+aa)){
            $("txt"+aa).value = start+n;
            n++;
            value++;
        }else {
            break;
        }
    }
    return;
}
//向上关联
function prev_express(value){
    if ($("txt"+value).value == ''){
        alert("请扫描快递单号");
        return;
    }
    var str = $("txt"+value).value;

    var express_company_no = '<{$express_company_no}>';
    if (express_company_no){
        switch(express_company_no){
            case 'EMS'://EMS
                if (isEMSNo(str)){
                    enterPrevNo(value,express_company_no);
                    return ;
                }
                break;
            case 'SF'://顺丰
            	if (isSFNo(str)){
                  enterPrevNo(value,express_company_no);
                  return ;
            	}
                break;
            case 'ZJS'://宅急送
            	if (isZJSNo(str)){
	                enterPrevNo(value,express_company_no);
	                return ;
            	}
                break;
        }
    }

    var end = str.substr(-1);
    if(!is_num(end)){
        alert('请输入正确的快递单号!');
        return ;
    }

    num = str.match(/[1-9]\d*\b/g);
    len = str.lastIndexOf(num);
    start = str.substring(0,len);
    n = new Number(num)-1;
    while (true){
        var bb = value-1;
        if ($("txt"+bb)){;
            if(n>=0){
                $("txt"+bb).value = start+n;
            
            }else{
                $("txt"+bb).value = '';
            }
            n--;
            value--;
        }else {
            break;
        }
    }
    return;
}

//向下关联单号填充
function enterNextNo(btn,company_no){
    var value = btn;
    while (true){
        var aa = value+1;
        if ($("txt"+aa)){
            switch(company_no){
                case 'EMS':
                	$("txt"+aa).value = getEMSNext($('txt'+value).value);
                    break;
                case 'SF':
                	$("txt"+aa).value = getSFNext($('txt'+value).value);
                    break;
                case 'ZJS':
                    $("txt"+aa).value = getZJSNext($('txt'+value).value);
                    break;
            }
            value++;
        }else {
            break;
        }
    }
    return ;
}
//向上关联单号填充
function enterPrevNo(btn,company_no){
    value = btn;
    while (true){
        var aa = value-1;
        if ($("txt"+aa)){
            switch(company_no){
                case 'EMS':
                    $("txt"+aa).value = getEMSPrev($('txt'+value).value);
                    break;
                case 'SF':
                    alert('不支持向上关联');
                    return;
                    //$("txt"+aa).value = getSFPrev($('txt'+value).value);
                    break;
                case 'ZJS':
                    $("txt"+aa).value = getZJSPrev($('txt'+value).value);
                    break;
            }
            value--;
        }else {
            break;
        }
    }
    return ;
}


//-------------------------------EMS快递单号关联
function isEMSNo(no){
    if (no.length != 13){
        return false;
    }
    if (getEMSLastNum(no) == no.substr(10,1))
        return true;
    else
        return false;
}
function getEMSLastNum(no){
    var v = new Number(no.substr(2,1)) * 8;
    v += new Number(no.substr(3,1)) * 6;
    v += new Number(no.substr(4,1)) * 4;
    v += new Number(no.substr(5,1)) * 2;
    v += new Number(no.substr(6,1)) * 3;
    v += new Number(no.substr(7,1)) * 5;
    v += new Number(no.substr(8,1)) * 9;
    v += new Number(no.substr(9,1)) * 7;
    v = 11 - v % 11;
    if (v == 10)
        v = 0;
    else if (v == 11)
        v = 5;
    return v.toString();
}
//上一个快递单号
function getEMSPrev(no){
    var serialNo = new Number(no.substr(2,8));
    if (serialNo > 0)
        serialNo--;
    strSerialNo = pad(serialNo, 8);
    temp = no.substr(0,2) + strSerialNo + no.substr(10);
    temp = no.substr(0,2) + strSerialNo + getEMSLastNum(temp) + no.substr(11);
    return temp;
}
//下一个快递单号
function getEMSNext(no){
    var serialNo = parseInt(no.substr(2,8));
    if (serialNo < 99999999)
        serialNo++;
    strSerialNo = pad(serialNo, 8);
    temp = no.substr(0,2) + strSerialNo + no.substr(10);
    temp = no.substr(0,2) + strSerialNo + getEMSLastNum(temp) + no.substr(11);
    return temp;
}


//-----------------------------顺丰快递单号
//验证是否顺丰快递单号
function isSFNo(no){
    if (!is_num(no)){
        return false;
    }else{
        return true;
    }
}
//上一个快递单号
function getSFPrev(no){
    return false;
}
//下一个快递单号
function getSFNext(ShunFengNo){
	var fri,Nfri,Yuandanhao;
    var res;
    var num1,num2,num3,num4,num5,num6,num7,num8,num9,num10,num11,num12;
    var Nnum1,Nnum2,Nnum3,Nnum4,Nnum5,Nnum6,Nnum7,Nnum8,Nnum9,Nnum10,Nnum11,Nnum12;
    var mid,I,ShunFengres;

    ShunFengres = '';
    fri = ShunFengNo.substr(0,11);
    Yuandanhao = ShunFengNo;
    //先将前11位加1，存储为新前11位
    Nfri = String(parseInt(fri)+1);
    //获取原始前11位
    num1 = parseInt(Yuandanhao.substr(0,1));
    num2 = parseInt(Yuandanhao.substr(1,1));
    num3 = parseInt(Yuandanhao.substr(2,1));
    num4 = parseInt(Yuandanhao.substr(3,1));
    num5 = parseInt(Yuandanhao.substr(4,1));
    num6 = parseInt(Yuandanhao.substr(5,1));
    num7 = parseInt(Yuandanhao.substr(6,1));
    num8 = parseInt(Yuandanhao.substr(7,1));
    num9 = parseInt(Yuandanhao.substr(8,1));
    num10 = parseInt(Yuandanhao.substr(9,1));
    num11 = parseInt(Yuandanhao.substr(10,1));
    num12 = parseInt(Yuandanhao.substr(11,1));
    //获取新前11位
    Nnum1 = parseInt(Nfri.substr(0,1));
    Nnum2 = parseInt(Nfri.substr(1,1));
    Nnum3 = parseInt(Nfri.substr(2,1));
    Nnum4 = parseInt(Nfri.substr(3,1));
    Nnum5 = parseInt(Nfri.substr(4,1));
    Nnum6 = parseInt(Nfri.substr(5,1));
    Nnum7 = parseInt(Nfri.substr(6,1));
    Nnum8 = parseInt(Nfri.substr(7,1));
    Nnum9 = parseInt(Nfri.substr(8,1));
    Nnum10 = parseInt(Nfri.substr(9,1));
    Nnum11 = parseInt(Nfri.substr(10,1));
    if ( Nnum9-num9==1 && (num9)%(2)==1 ){
      if (num12-8 >= 0)
         Nnum12 = num12-8             // -8
      else
         Nnum12 = num12-8 +10
    }else if( Nnum9-num9==1 && (num9)%(2)==0 ){
      if (num12-7 >=0)
         Nnum12 = num12-7             // -7
      else
         Nnum12 = num12-7 +10
    }else{
      if( ((num10==3) || (num10==6))&& (num11==9) ){
        if (num12-5 >=0)
           Nnum12 = num12-5             // -5
        else
           Nnum12 = num12-5 +10
      }else if (num11==9){
        if (num12-4 >=0)
           Nnum12 = num12-4             // -4
        else
           Nnum12 = num12-4 +10
      }else{
        if (num12-1 >=0)
           Nnum12 = num12-1            // -1
        else
           Nnum12 = num12-1 +10
      }
    }
    ShunFengres = Nfri+String(Nnum12);
    return ShunFengres;
}

//-----------------------------宅急送递单号
function isZJSNo(no){
  if (!is_num(no)){
      return false;
  }else{
      return true;
  }
}
function getZJSPrev(no){
   var retNo = parseInt(no);
   if (retNo % 10 == 0)
       retNo -= 4;
   else
       retNo -= 11;
   return pad(retNo, no.length);
}
function getZJSNext(no){
   var retNo = parseInt(no);
   retNo += 11;
   if (retNo % 10 > 6){
	   retNo -= 7;
   }
   return pad(retNo, no.length);
}

function pad(num, n) {
    return Array(Math.abs((''+num).length-(n+1))).join(0)+num;
}
function is_num(str){
    var pattrn = /^[0-9]+$/;
    if (pattrn.test(str)){
        return true;
    }else{
        return false;
    }
}
</script>

<{ if $errIds }>
<div class="errormsg notice" id="errormsg">
    <div id="msg" class="msg">注意：本次打印数据中的一些单据有问题，这些数据将被忽略(详细内容见下面列表)！！！</div>
    <br>
    <{ foreach from=$errIds item=id }>
        <{$allItems[$id]['delivery_bn']}> &nbsp; : &nbsp; <{$errInfo[$id]}><br>
    <{ /foreach }>
</div>
<{ /if }>

<{if $err=='false'}>
<div class="">
<div class="order-box">
<form name="form1" id="form1" method="POST" action="index.php?app=ome&ctl=admin_receipts_print&act=insertExpress&finder_id=<{$env.get.finder_id}>">
<table width="100%" border="0" cellpadding="0" cellspacing="0" style="border-bottom:1px solid #ddd">
<tr class="order-tr"><td><b>打印快递单</b></td></tr>
<tr> <td class="d_th">请扫描快递单号：(注意：快递单号必须为数字)&nbsp;</td><td><input type="checkbox" name="c1" id="cc" /><span id="sp_1">关闭上下关联</span></td><td></td><td width="50%"></td></tr>
<{foreach from=$idd key=key item=item}>
<tr><td class="d_td" ><{$item.delivery_bn}>:</td><td>
    <input type="text" class="logi_no_input" vtype="number&&required" name="id[<{$item.delivery_id}>]" id="txt<{++$key}>" value="<{$logid[$item.delivery_id]}>" consignee_name="<{$items[$item.delivery_id].consignee.name}>" ident="<{$idents[$item.delivery_id]}>" logi_no="<{$logid[$item.delivery_id]}>" />&nbsp;&nbsp;</td>
    <td><!-- 收货人:<{$items[$item.delivery_id].consignee.name}> 批次号:<{$idents[$item.delivery_id]}> --></td>
    <td><{if count($idd) != 1}><{if $key == 1}><{img style="display:none" id="bbaa{$key}" src="bundle/icon_desc.gif" onclick="next_express({$key})" title="向下关联" app="desktop"}><{elseif $key == count($idd)}><{img style="display:none" id="bbaa{$key}" src="bundle/icon_asc.gif" onclick="prev_express({$key})" title="向上关联" app="desktop"}><{else}><{img style="display:none" id="bbaa{$key}" src="icons/icon_updown.gif" onclick="updown_express({$key})" title="上下关联" app="ome"}><{/if}><{if $key !==1 && $key !== count($idd)}> &nbsp; <{img src="bundle/icon_asc.gif" id="bbaa{$key}" style="display:none" title="向上关联" onclick="prev_express({$key})" app="desktop"}> &nbsp; <{img src="bundle/icon_desc.gif" style="display:none" id="bbaa{$key}" title="向下关联" onclick="next_express({$key})" app="desktop"}><{/if}><{/if}></td>
    <td></td>
  </tr>
<{/foreach}>
<tr> <td class="d_th"><{button label="确认" class="btn-primary" type="button" onClick="check()" id="b2" }>&nbsp;</td>
<td></td><td></td></tr>

</table>
</form>

<input type="hidden" name='printname' id='printname' value="<{$vid}>">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td>
 <div class="note">
 <h5>快递单打印预览：</h5>
    上偏移:<{$prt_tmpl.prt_tmpl_offsety|default:0}>mm,
    左偏移:<{$prt_tmpl.prt_tmpl_offsetx|default:0}>mm,
    纸张宽:<{$prt_tmpl.prt_tmpl_width}>mm,
    纸张高:<{$prt_tmpl.prt_tmpl_height}>mm,
 </div>
 <div id="dly_printer" style="height:<{$prt_tmpl.prt_tmpl_height*$dpi/25.4*$order_number}>px;width:<{$prt_tmpl.prt_tmpl_width*$dpi/25.4}>px;">
</div>
</td>
  </tr>
</table>
</div>
</div>
<{/if}>

<textarea id="print_data" style="display:none">
<{$data}>
</textarea>

<div id="express_tools">
    <div id="express_tools_hidden">定位</div>
    <div id="express_tools_main">
	    <div class="express_tools_main_head clearfix">
         <div class="dialog-title flt">定位</div>
          <a href="javascript:void(0)" class="btn-close frt">✖</a>
        </div>
	    <div>
	    根据：<select id="search_option">
		    <option value="logi_no">物流单号</option>
		    <option value="consignee_name">收货人</option>
		    <option value="ident">批次号</option>
	    </select>
	    <input type="text" name="keywords" id="keywords" /></div>
	    <div><{button label="搜索" class="btn-primary" type="button" onClick="search_logi_no()" }>
	    </div>
    </div>
</div>
<script>

function changePrint()
{
    new Dialog(new Element("div.tableform",{html:'<div id="pause" class="division">正在提交...<{img app="desktop" src="loading.gif"}></div><div class="table-action"><{button label="关闭" onclick="re_finder();"}></div>'}),{
        title:'提示',
        width:230,
        height:130,
        modal:true,
        resizeable:false}
    );
    var printname= $('printname').value;
    new Request({url:'index.php?app=ome&ctl=admin_receipts_print&act=setPrintStatus',method:'post',data:'type=express&str='+printname,
        onSuccess:function(json){
          if (json == 'true'){
              $('pause').getParent('.dialog').retrieve('instance').close();
              $('dly_printer_flash').doPrint();
          }else {
              $('pause').set('text',json);
          }
        }
    }).send();
}

var swf = new Swiff('<{$base_dir}>/app/ome/upload/images/printermode.swf?<{$smarty.now}>', {
    width:  '100%',
    height: '100%',
    params:{wMode:false},
    id:'dly_printer_flash',
    container: $('dly_printer'),
    vars:{
    xml:'<{$prt_tmpl.prt_tmpl_data}>',
    data:$("print_data").value <{if $tmpl_bg}>,
    bg:'<{$tmpl_bg}>'<{/if}>,
    order_number:'<{$order_number}>',
    offset_y:'<{$prt_tmpl.prt_tmpl_offsety|default:0}>',
    offset_x:'<{$prt_tmpl.prt_tmpl_offsetx|default:0}>',
    page_w:'<{$prt_tmpl.prt_tmpl_width}>',
    page_h:'<{$prt_tmpl.prt_tmpl_height}>',
    copyright:'shopex'}
});

/*flash call*/
var printermode_complete_callback = function(){

        $('dly_printer').empty().set('html',"<div class='success'>任务已成功发送至打印机!</div>");
        $('doprint_btn').dispose();

}

var printermode_ready_callback = function(){


}

window.addEvent('domready', function() {            
    
   siderExpressToolsSetGoTop();  
   
   $('express_tools_main').hide();
   
   $('express_tools_hidden').addEvent('click',function(){
	   $('express_tools_main').show();
	   $('express_tools_hidden').hide();
   });
   
   $E('.btn-close',$('express_tools_main')).addEvent('click',function(){
       $('express_tools_main').hide();
       $('express_tools_hidden').show();
   });
   
   $('keywords').addEvent('enter',function(e){
	    search_logi_no();
	});
});

$('main').addEvent('scroll',function(){  
    siderExpressToolsSetGoTop();
})  

function siderExpressToolsSetGoTop(){
    $('express_tools').tween('top',$('main').getScroll().y+100)
} 



var last_search_res = {};
function search_logi_no(){
	var keywords = $('keywords').value.trim();
    if( keywords == ''){
    	MessageBox.error('请填写关键字!');
        return false;
    }
    
    var search_res = [];
    var check_index = -1;
    $ES('.logi_no_input',$('form1')).each(function(el,index){
    	 if(keywords == el.get($('search_option').value)){
    		 search_res.push(index);
    		 if(check_index == -1){
    			 check_index = index;
    		 }
         }
    });
    
    if(search_res.length > 2 && last_search_res.check_index != -1){
        if(search_res[search_res.length-1] == last_search_res.check_index){
            check_index = search_res[0];
        }else{
            for(var i=0;i<search_res.length;i++){
                 if(search_res[i] > last_search_res.check_index){
                     check_index = search_res[i];
                     break;
                 }
             }  
        }
    }
    
    if(check_index != -1){
    	
	    $ES('.logi_no_input',$('form1')).each(function(el,index){
	        if(check_index == index){
	            el.getParent().getParent().set('bgcolor','#ffffcc');
	            check_index_y = el.getPosition().y;
	        }else{
	            el.getParent().getParent().set('bgcolor','#ffffff');
	        }
	    });
	    $('main').scrollTo(0,check_index_y);
    }else{
    	$ES('.logi_no_input',$('form1')).each(function(el,index){
    		el.getParent().getParent().set('bgcolor','#ffffff');
        });
    	MessageBox.error('没有定位目标!');
    }
    
    last_search_res = {'keywords':keywords,'check_index':check_index};
}

/*function empty_logi_no(){
	if(last_search_res.check_index == -1){
		alert('还未定位目标！');
		return false;
	}
	
	$ES('.logi_no_input',$('form1')).each(function(el,index){
        if(last_search_res.check_index < index){
            el.value = '';
        }
    });
}*/



</script>
<{capture name="footbar"}>
<div class="table-action">
    <{button label="打印快递单" onclick="changePrint()" id="doprint_btn"}>
</div>
<{/capture}>
